<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/5
 * Time: 15:43
 */
namespace app\admin\controller;

use think\controller;

class Goods extends Admin
{
    protected $model;

    public function _initialize()
    {
        $this->model = model('am_goods');
    }

    //商品列表页面
    public function goodsList()
    {
        $res = $this->model->seleteAll();
        $this->assign('res',$res);
        return view('goods_list');
    }

    //商品添加页面
    public function goodsAddPage()
    {
        $res = $this->model->seleteAll();
        $this->assign('res',$res);
        return view('goods_add_page');
    }

    //商品添加
    public function goodsAdd()
    {
        $data['name'] = input('name');
        $data['price'] = input('price');
        $data['avatar'] = input('avatar');
        $data['keywords'] = input('keywords');
        $data['c_id'] = input('c_id');
        $data['status'] = input('status');
        $data['founder_id'] = input('founder_id');
        $res = $this->model->addGoods($data);
        if($res>0){
            echo "<script>alert('添加成功');window.location.href = 'goodslist'</script>";
        }else{
            echo "<script>alert('添加失败');window.history.go(-1)</script>";
        }
    }

    //商品修改页面
    public function goodsUpdatePage()
    {
        $id = input('id');
        $res = $this->model->updateGoods($id);
        $this->assign('res',$res);
        return view('goods_update_page');
    }

    //商品更新
    public function goodsUpdate()
    {
        $id = input('id');
        $data['name'] = input('name');
        $data['price'] = input('price');
        $data['avatar'] = input('avatar');
        $data['keywords'] = input('keywords');
        $data['c_id'] = input('c_id');
        $data['status'] = input('status');
        $data['founder_id'] = input('founder_id');
        $res = $this->model->editGoods($data,$id);
        if($res>0){
            echo "<script>alert('更新成功');window.location.href = 'goodslist'</script>";
        }else{
            echo "<script>alert('更新失败');window.history.go(-1)</script>";
        }
    }

    //商品删除
    public function goodsDelete()
    {
        $id = input('id');
        $res = $this->model->deleteGoods($id);
        if($res>0){
            echo "<script>alert('删除成功');window.location.href = 'goodslist'</script>";
        }
    }
}